home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Networking / PCCardNetworkSample / PortScanner.c < prev    next >
Encoding:
Text File  |  2000-09-28  |  4.9 KB  |  167 lines  |  [TEXT/CWIE]

  1. /*
  2.     File:        PortScanner.c
  3.  
  4.     Contains:    Before using this code, check out the document "OTAdvancedClientProg1.0b1"
  5.                 which is available as part of the Open Transport SDK - specifically read
  6.                 the section regarding Open Transport Port Scanners.  A copy of the OT SDK
  7.                 is available at 
  8.     
  9.                 <http://developer.apple.com/macos/opentransport/>
  10.     
  11.                 You will find in this sample, 2 calls which are exported, one to register
  12.                 an OT port and the second to unregister the port.  These calls are
  13.                 implemented here and not in the code bases which might directly use them
  14.                 to circumvent loading problems if OT is not present.  For example a USB
  15.                 Ethernet driver shim would want to register it's port, however, since a
  16.                 USBShim is loaded before OT is present, CFM would either fail to load
  17.                 the shim if OT was hard linked, or OT calls would not function from the
  18.                 shim if OT was weak linked.  The latter case occurs because at the time
  19.                 the library is loaded, if CFM cannot resolve a weak linked fragment, the
  20.                 links to those dependent library calls do not get fixed up, even though
  21.                 later on, those library services become available.  Note that the above is
  22.                 also true for a PC card Enabler, which is the reason for this sample.
  23.     
  24.                 The OT RegisterPort call can be made in the OT PortScanner code here, since
  25.                 the port scanner will always be called after OT is loaded. 
  26.  
  27.     Written by: Original port scanner code written by:    Quinn "The Eskimo!"
  28.                 Modified to support PC Cards by Rich Kubota    
  29.  
  30.     Copyright:    Copyright © 1998-1999 by Apple Computer, Inc., All Rights Reserved.
  31.  
  32.                 You may incorporate this Apple sample source code into your program(s) without
  33.                 restriction. This Apple sample source code has been provided "AS IS" and the
  34.                 responsibility for its operation is yours. You are not permitted to redistribute
  35.                 this Apple sample source code as "Apple sample source code" after having made
  36.                 changes. If you're going to re-distribute the source, we require that you make
  37.                 it clear in the source that the code was descended from Apple sample source
  38.                 code, but that you've made changes.
  39.  
  40.     Change History (most recent first):
  41.                 8/16/1999    Karl Groethe    Updated for Metrowerks Codewarror Pro 2.1
  42.                 
  43.  
  44. */
  45.  
  46. //----------------------------------------------------------------------
  47.  
  48. #define    DEBUG                    1
  49. //----------------------------------------------------------------------
  50.  
  51. #include <OpenTptModule.h>
  52. #include <OpenTptLinks.h>
  53. #include <PCCardEnablerPlugin.h>
  54. #include <PCCardTuples.h>
  55. #include <Gestalt.h>
  56. #include <DriverFamilyMatching.h>
  57. #include "EnablerSample.h"
  58. #include "MyRegisterPort.h"
  59.  
  60. // the following structure is required to register a PCI port
  61.  
  62.  
  63. /////////////////////////////////////////////////////////////////////
  64.  
  65.  
  66. extern void     OTScanPorts(UInt32 scanType);
  67. extern Boolean    CanRegisterThisPort(RegEntryID *deviceRef);
  68.  
  69. /*
  70.     Determine whether we need to register the port for this card.
  71.     This check is made by verifying that the target device has a 
  72.     "port-configured" property, which the enabler that goes along
  73.     with this project creates, when it processes the AddDeviceProperties
  74.     call gets made.
  75. */
  76. Boolean     CanRegisterThisPort(RegEntryID *deviceRef)
  77. {
  78.     UInt32        size;
  79.     OSStatus    err;
  80.     UInt16        portConfigProperty;
  81.     
  82.     size = sizeof(UInt16);
  83.     err = RegistryPropertyGet(deviceRef, kPortConfigured, 
  84.                                 &portConfigProperty, &size);
  85.  
  86.         // if an error occurred, then the property does not exist or the property
  87.         // is a different size, so don't configure this card    
  88.     if (err != noErr)
  89.     {
  90.         return false;
  91.     }
  92.             // check that the value is 0, meaning that we haven't 
  93.             // registered the port associated with this card
  94.             // 
  95.     else if (portConfigProperty != 0)
  96.         return false;
  97.     else
  98.         return true;
  99. }
  100.  
  101. #pragma export on
  102.  
  103.  
  104. /*
  105.     The OTScanPorts symbol is the required symbol to be exported for a port scanner.
  106. */
  107.  
  108. #pragma export list OTScanPorts
  109.  
  110. extern void OTScanPorts(UInt32 scanType)
  111. {
  112.     RegEntryIter    cookie;
  113.     RegEntryID        deviceID;
  114.     OSType            cardType = 'pccd';
  115.     OSStatus        err;
  116.     Boolean            done = false;
  117.     
  118.         // the port scanner only handles the initial scan after system startup.
  119.     if (scanType != kOTInitialScan)
  120.     {
  121. #if DEBUG
  122.         DebugStr("\p scanType != kOTInitialScan");
  123. #endif
  124.         return;
  125.     }
  126.     
  127.     if ( RegistryEntryIterateCreate(&cookie) != noErr )
  128.     {
  129. #if DEBUG
  130.         DebugStr("\p RegistryEntryIterateCreate in OTScanPorts failed");
  131. #endif
  132.         return;
  133.     }
  134.     
  135. #if DEBUG
  136.         DebugStr("\p entering OTScanPorts do loop");
  137. #endif
  138.     do
  139.     {
  140.         err = RegistryEntrySearch(&cookie, kRegIterContinue, &deviceID, &done, 
  141.                                     kNodeTypePropertyName, &cardType, sizeof(cardType));
  142.             
  143.             // check to see if we are done searching
  144.         if (err != noErr) 
  145.         {
  146. #if DEBUG
  147.             DebugStr("\p error calling RegistryEntrySearch");
  148. #endif
  149.             break;
  150.         }
  151.         
  152.         if (CanRegisterThisPort(&deviceID) == true)
  153.         {
  154.             RegisterThePort(&deviceID);
  155.         }
  156.         else
  157.         {
  158. #if DEBUG
  159.             DebugStr("\p CanRegisterThisPort returned false");
  160. #endif
  161.         }
  162.     }
  163.     while (done == false);
  164. }
  165.  
  166. #pragma export off
  167.